package com.guisu.material.recyclerview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import com.guisu.material.R

/**
 * on 2022/11/7
 */
class CommonRefreshCreator : RefreshViewCreator() {
    private var refreshView: View? = null

    override fun getRefreshView(context: Context, parent: ViewGroup): View {
        val view =
            LayoutInflater.from(context).inflate(R.layout.layout_progressbar, parent,false)
        refreshView = view.findViewById(R.id.pb)
        return view
    }

    override fun onPull(currentDragHeight: Int, refreshViewHeight: Int, currentRefreshStatus: Int) {
        val rotate = currentDragHeight / refreshViewHeight
        //不断下拉的过程中不断旋转图片
        refreshView!!.rotation = (rotate * 360).toFloat()
    }

    override fun onRefreshing() {
        //刷新的时候也不断旋转
        val animation = RotateAnimation(
            0f,
            720f,
            Animation.RELATIVE_TO_SELF,
            0.5F,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        animation.repeatCount = -1
        animation.duration = 2000
        refreshView!!.startAnimation(animation)
    }

    override fun onStopRefresh() {
        //停止的时候清除动画
        refreshView!!.rotation = 0f
        refreshView!!.clearAnimation()
    }
}

